# Contributor:Andremosfet <andrejfet at hotmail dot it>

pkgname=xbmc-pvr-testing
pkgver=30050
pkgrel=1
pkgdesc="XBMC Media Center"
arch=('i686' 'x86_64')
url="http://xbmc.org"
license=('GPL' 'custom')
depends=('alsa-lib' 'curl' 'enca' 'faac' 'freetype2' 'fribidi' 'gawk' 'glew'
         'hal' 'jasper' 'libgl' 'libjpeg>=7' 'libpng>=1.4' 'libmad' 'libmysqlclient' 'ffmpeg'
         'libxinerama' 'libxrandr' 'lzo2' 'sdl_image>=1.2.10' 'sdl_mixer' 'sqlite3'
         'tre' 'unzip' 'xorg-server' 'libcdio' 'wavpack' 'libmpeg2' 'a52dec'
         'libdca' 'smbclient' 'faad2' 'libsamplerate' 'libmms' 'xorg-utils' 'cvs' 'libmodplug')
makedepends=('autoconf' 'automake' 'boost' 'cmake' 'gcc' 'gperf'
            'libtool>=2.2.6a-1' 'make' 'nasm' 'patch' 'pkgconfig' 'zip'
            'libvdpau' 'subversion' 'libmicrohttpd' 'flex' 'bison')
optdepends=('lirc: remote controller support'
	    'smbclient: access windows shares'
	    'unrar: access compressed files without unpacking them'
	    'devicekit-power: used to trigger suspend functionality')
provides=('xbmc')
conflicts=('xbmc' 'xbmc-crystalhd' 'ixbmc-trunk')
install=("${pkgname}.install")
source=(FEH.sh 
        projectM.diff)
md5sums=('c3e2ab79b9965f1a4a048275d5f222c4'
         '70eed644485de10cb80927bc1a3c77c7')

_prefix=/usr

build() {
    MAKEFLAGS="-j1"

    _svntrunk=https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/pvr-testing2/
    _svnmod=XBMC

    cd ${srcdir}/
    if [ -d $_svnmod/.svn ]; then
        (cd $_svnmod && svn up -r $pkgver) || return 1
    else
        svn co $_svntrunk --config-dir ./ -r $pkgver $_svnmod || return 1
    fi
    msg "SVN checkout done or server timeout"
    msg "Starting make..."

    cp -r "$srcdir/$_svnmod" "$srcdir/$_svnmod-build"
    cd "$srcdir/$_svnmod-build"

    
    # Patch for missing projectM presets
   patch -p0 -i ../projectM.diff || return 1

    # Archlinux Branding by SVN_REV
    export SVN_REV="$pkgver-ARCH"

    # fix lsb_release dependency
    sed -i -e 's:/usr/bin/lsb_release -d:cat /etc/arch-release:' xbmc/utils/SystemInfo.cpp || return 1



# SSE2 -> SSE
msg "SSE2 -> SSE"
sed -i 's/CXXFLAGS+=-I. -DSQUISH_USE_SSE=2 -msse2/CXXFLAGS+=-I. -DSQUISH_USE_SSE=1 -msse/' ./xbmc/lib/libsquish/Makefile.in



    # Configure XBMC
    msg "Configuring XBMC" 
    ./bootstrap || return 1

    ./configure --prefix=${_prefix} \
                --enable-vdpau \
                --enable-vaapi \
                --enable-dvdcss \
                --disable-external-ffmpeg \
                --disable-external-python \
                --disable-external-libass \
                --disable-debug \
                --disable-asap-codec || return 1
    
    # Now (finally) build
    msg "Running make" 
    make -j1 || return 1

    make prefix=${pkgdir}/usr install || return 1

    # Fix the shell script
    #sed -i '2iexport SDL_AUDIODRIVER=alsa' ${pkgdir}/usr/bin/xbmc

    # Replace FEH.py with FEH.sh (and thus remove external python dependency)
    install -Dm755 ${srcdir}/FEH.sh \
      ${pkgdir}${_prefix}/share/xbmc/FEH.sh || return 1

    sed -i -e "s/python \\${_prefix}\/share\/xbmc\/FEH.py \"\$@\"/\\${_prefix}\/share\/xbmc\/FEH.sh/g" \
      ${pkgdir}${_prefix}/bin/xbmc || return 1

    # lsb_release fix
    sed -i -e 's/which lsb_release &> \/dev\/null/\[ -f \/etc\/arch-release ]/g' \
      ${pkgdir}${_prefix}/bin/xbmc || return 1

    sed -i -e "s/lsb_release -a 2> \/dev\/null | sed -e 's\/\^\/    \/'/cat \/etc\/arch-release/g" \
      ${pkgdir}${_prefix}/bin/xbmc || return 1

    # Xrandr
    install -Dm755 ${srcdir}/$_svnmod-build/xbmc-xrandr \
                   ${pkgdir}/usr/share/xbmc/xbmc-xrandr || return 1

    # Menu item
    install -Dm644 ${srcdir}/$_svnmod-build/tools/Linux/xbmc.desktop \
                   ${pkgdir}/usr/share/applications/xbmc.desktop || return 1
    install -Dm644 ${srcdir}/$_svnmod-build/tools/Linux/xbmc.png \
                   ${pkgdir}/usr/share/pixmaps/xbmc.png || return 1

    # TexturePacker 
    install -Dm755 ${srcdir}/$_svnmod-build/tools/TexturePacker/TexturePacker \
                   ${pkgdir}/usr/share/xbmc/tools/texturepacker || return 1

    # License(s)
    install -dm755 ${pkgdir}/usr/share/licenses/${pkgname}
    for licensef in LICENSE.GPL README.linux copying.txt; do
        mv ${pkgdir}/usr/share/xbmc/${licensef} \
           ${pkgdir}/usr/share/licenses/${pkgname} || return 1
    done
}
